<?php
class Integral_ruleAction extends IntesysAction {
	function index(){
		$formDB=M('Form');
		$this->initView();
		//优先查询关键字，其次为规则类型
		if($this->_param('keywords')){
			$keywords=trim($this->_param('keywords'));
			$where['r.name']=array('like','%'.$keywords.'%');
			$where['r.remark']=array('like','%'.$keywords.'%');
			$where['_logic']='or';
			$map['_complex']=$where;
		}
		$map['r.site_id']=__SITEID__;
		if($this->_param('type_id')){
			$typeId=implode(',',D('Integral_ruletype')->getTypeId($this->_param('type_id')));
			if(empty($typeId)){
				$map['r.type_id']=$this->_param('type_id');
			}else{
				$map['r.type_id']=array('in',$this->_param('type_id').','.$typeId);
			}
		}

		$count=D('integral_rule')->alias('r')->where($map)->count();
		import('ORG.Util.Page');
		$p = new Page($count,10);
		$list=D('Integral_rule')->alias('r')->join(C('DB_PREFIX').'integral_ruletype t on t.id = r.type_id')->where($map)->limit($p->firstRow . ',' . $p->listRows)->field('r.*,t.name as type_name')->select();
//		if($this->_param('type_id'))			var_dump(D('Integral_rule')->getLastSql());

		$this->assign('list',$list);
		$integraltype = M('integral_ruletype')->where(array('site_id'=>__SITEID__))->select();
		$this->assign('ruletypelist',$integraltype);

		
		$this->specialForm=$formDB->where('site_id='.__SITEID__.' and special>0')->select();
		$this->particularForm=$formDB->where('site_id='.__SITEID__)->select();//intePanel
		$this->formcategory = M('Formcategory') -> where(array('site_id' => __SITEID__)) -> order('sort, id desc') -> select();
		$pages = $p->show();
		$this->assign('pages', $pages);
		$this->display();
	}
	function test(){
		dump(implode(',',D('Integral_ruletype')->getTypeId(0)));
	}
	//修改页面
	function edit(){
	    $this->assign('integraltypelist',D('Integral_type')->getType());
		$this->assign('ruletypelist',D('Integral_ruletype')->getList());
		parent::edit();	
	}
	//添加页面
	function add(){
	    $this->assign('integraltypelist',D('Integral_type')->getType());
		$this->assign('ruletypelist',D('Integral_ruletype')->getList());
		$this->display();	
	}
	//插入新的数据
	function insert(){ 
		
		if(IS_POST){
			//将规则内容转化为符合格式的数据对象,优先为详细的数据对象
			$judge=true;
			foreach($_POST['source'] as $v){
				$ret=implode(array_values($v));
				$judge=$judge?empty($ret):false;
			}
			if($judge){
				$_POST['source']=serialize($_POST['inte']);
			}else{
				$_POST['source']=serialize($_POST['source']);
			}
			unset($_POST['inte']);
			$_POST['site_id']=__SITEID__;
			$_POST['member_id']=$this->UserInfo;
			parent::insert();	
		}
	}
	function update(){
		if(IS_POST){
			$judge=true;
			foreach($_POST['source'] as $v){
				$ret=implode(array_values($v));
				$judge=$judge?empty($ret):false;
			}
			if($judge){
				$_POST['source']=serialize($_POST['inte']);
			}else{
				$_POST['source']=serialize($_POST['source']);
			}
			unset($_POST['inte']);
			$_POST['member_id']=$this->UserInfo;
			parent::update();	
		}	
	}
	//获取其中一个规则
    function getruleone(){
		if($this->_get('id')){
			$integralrule=$this->dao->where(array('id'=>$this->_get('id')))->find();
			echo json_encode(array('integralruleinfo'=>$integralrule,'statu'=>1));
		}  
    }
	//获取某个类型的规则
    function getrule(){
		if(IS_POST){
			$typeid=$this->_post('typeid');	
			$integralrule=$this->dao->where(array('site_id'=>__SITEID__,'type_id'=>$typeid))->select();
			echo json_encode(array('integralrule'=>$integralrule,'statu'=>1));
		}  
    }
	//获取某个部门的用户
    function getmember(){
		if($this->_get('groupid')){
			$groupid=$this->_get('groupid');	
			$member=D("Member")->getgroupmember($groupid);
			echo json_encode(array('member'=>$member,'statu'=>1));
		}  
    }
	//获取某个部门的用户（不包括当前用户）
    function getmember2(){
		if($this->_get('groupid')){
			$groupid=$this->_get('groupid');	
			$member=D("Member")->getgroupmember2($groupid);
			echo json_encode(array('member'=>$member,'statu'=>1));
		}  
    }
	//获取用户的部门
	function getmembergroup(){
		if($this->_get('id')){
			$groupid=M('Member')->alias('m')->join('left join __MEMBER_GROUP__ g on g.id=m.group_id')->join('left join __MEMBER_GROUP__ g2 on g.pid=g2.id')->where(['m.id'=>$this->_get('id')])->getField('g2.id');	
			echo json_encode(['groupid'=>$groupid,'statu'=>1]);
		}	
	}
	//选择列表
	public function select(){
		$this->pagesize = 6;
		$this->rollpage = 3;
		$this->listmap = "site_id = ". __SITEID__;
		if($this->_get('selected') && $this->_get('selected') != ''){
			$this->pagesize = 100;
			$this->listmap = "id in(".$this->_get('selected').")";
		}
		if($this->_param('type_id')){
			$typeId=implode(',',D('Integral_ruletype')->getTypeId($this->_param('type_id')));
			if($typeId != ''){
				$typeId = ','.$typeId;
			}
			$this->listmap.=" and type_id in(".$this->_param('type_id').$typeId.")";
		}
		if($this->_param('keywords')){
			$keywords=$this->_param('keywords');
			$_GET['keywords']=urlencode($this->_param('keywords'));
			$this->listmap.=" and name like '%{$keywords}%'";
		}
		parent::_list();
		$this->integralType=M('Integral_type')->where('site_id='.__SITEID__)->getField('typename,remark');
		$this->display();
	}
	
	//获取‘所有规则’的视图
	public function getRuleView(){
		$this->initView();
		D('Integral_rule')->getRuleView($this->view);
	}
}

